namespace RushMail.Requests;

/// <summary>
/// 群发任务相关接口
/// </summary>
public class RushMailTask
{
    private readonly IRushMailClient _client;

    public RushMailTask(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 群发任务列表
    /// http://edmapi.rushmail.com/api.php?module=wc-task&action=task-list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<TaskListResponse>> List() =>
        (await _client.PostAsync(new RushMailRequest("wc-task", "task-list"))).ToGeneric<TaskListResponse>();

    /// <summary>
    /// 查看群发任务
    /// http://edmapi.rushmail.com/api.php?module=wc-task&action=task-view
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<TaskViewResponse>> View(int taskId) =>
        (await _client.PostAsync(new RushMailRequest("wc-task", "task-view")
        {
            Body = new Dictionary<string, string>() { { "task_id", taskId.ToString() } }
        }))
        .ToGeneric<TaskViewResponse>();

    /// <summary>
    /// 添加群发任务
    /// http://edmapi.rushmail.com/api.php?module=wc-task&action=task-add
    /// </summary>
    /// <returns></returns>
    public virtual async Task<RushMailResponse<int>> Add(TaskAdd body) =>
        (await _client.PostAsync(new RushMailRequest("wc-task", "task-add")
        {
            Body = body.ToDictionary()
        }))
        .ToGeneric<int>();

    /// <summary>
    /// 开始群发任务
    /// http://edmapi.rushmail.com/api.php?module=wc-task&action=task-start
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Start(int taskId) =>
        _client.PostAsync(new RushMailRequest("wc-task", "task-start")
        {
            Body = new Dictionary<string, string>() { { "task_id", taskId.ToString() } }
        });

    /// <summary>
    /// 中止/放弃群发任务
    /// http://edmapi.rushmail.com/api.php?module=wc-task&action=task-abort
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Abort(int taskId) =>
        _client.PostAsync(new RushMailRequest("wc-task", "task-abort")
        {
            Body = new Dictionary<string, string>() { { "task_id", taskId.ToString() } }
        });

    /// <summary>
    /// 删除群发任务
    /// http://edmapi.rushmail.com/api.php?module=wc-task&action=task-delete
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Delete(int taskId) =>
        _client.PostAsync(new RushMailRequest("wc-task", "task-delete")
        {
            Body = new Dictionary<string, string>() { { "task_id", taskId.ToString() } }
        });

}


public class TaskAdd
{
    public int TemplateId { get; set; }
    public int PoolId { get; set; }
    public string TaskName { get; set; }
    public int CountPreset { get; set; }
    public string TimePreset { get; set; }
    public int? EmailDomainId { get; set; }
    public int? EmailId { get; set; }
    public string ReplyTo { get; set; }
    /// <summary>
    /// 跟综统计 0 不使用 1 跟踪打开率 2 跟踪打开率和点击率
    /// </summary>
    public int Track { get; set; }
    /// <summary>
    /// -1 暂不发送 0 定时发送 -9 立即发送
    /// </summary>
    public int Status { get; set; }

    public virtual Dictionary<string, string> ToDictionary()
    {
        var result = new Dictionary<string, string>()
        {
            { "template_id", TemplateId.ToString()},
            { "pool_id", PoolId.ToString()},
            { "task_name", TaskName},
            { "count_preset", CountPreset.ToString()},
            { "time_preset", TimePreset },
            { "reply_to", ReplyTo },
            { "track", Track.ToString() },
            { "status", Status.ToString() },
        };
        if (EmailDomainId is not null)
            result.Add("email_domain_id", EmailDomainId.Value.ToString());

        if (EmailId is not null)
            result.Add("email_id", EmailId.Value.ToString());

        return result;
    }
}

public class TaskListResponse
{
    [JsonProperty("task_list")] public TaskListPage TaskList { get; set; }
}

public class TaskListPage
{
    public List<TaskItem> List { get; set; }
    public Paginate Page { get; set; }
}

public class TaskItem
{
    [JsonProperty("task_id")]
    public int TaskId { get; set; }
    [JsonProperty("template_id")]
    public int TemplateId { get; set; }
    [JsonProperty("pool_id")]
    public int PoolId { get; set; }
    [JsonProperty("category_id")]
    public int CategoryId { get; set; }
    [JsonProperty("task_name")] public string TaskName { get; set; }
    [JsonProperty("task_alias")] public string TaskAlias { get; set; }
    [JsonProperty("count_preset")] public string CountPreset { get; set; }
    [JsonProperty("count_send")] public string CountSend { get; set; }
    [JsonProperty("count_error")] public string CountError { get; set; }
    [JsonProperty("time_preset")] public string TimePreset { get; set; }
    [JsonProperty("time_start")] public string TimeStart { get; set; }
    /// <summary>
    /// 任务状态 -1 暂不发送 0 等待发送 1 正在发送 2 发送完成 3 暂停发送 4 取消发 送
    /// </summary>
    public int Status { get; set; }
    /// <summary>
    /// 审核状态 0 等待审核 1 审核通过 -1 未通过审核
    /// </summary>
    public int examine { get; set; }
}

public class TaskViewResponse
{
    public TaskViewItem Task { get; set; }
}

public class TaskViewItem
{
    [JsonProperty("task_id")] public int TaskId { get; set; }
    [JsonProperty("customer_id")] public int CustomerId { get; set; }
    [JsonProperty("account_id")] public int AccountId { get; set; }
    [JsonProperty("template_id")] public int TemplateId { get; set; }
    [JsonProperty("pool_id")] public int PoolId { get; set; }
    [JsonProperty("index_beg")] public int IndexBeg { get; set; }
    [JsonProperty("index_end")] public int IndexEnd { get; set; }
    [JsonProperty("task_name")] public string TaskName { get; set; }
    [JsonProperty("task_alias")] public string TaskAlias { get; set; }
    [JsonProperty("task_file")] public string TaskFile { get; set; }
    [JsonProperty("task_size")] public int TaskSize { get; set; }
    [JsonProperty("addrratio")] public object AddrRatio { get; set; }
    [JsonProperty("count_preset")] public int CountPreset { get; set; }
    [JsonProperty("count_send")] public int CountSend { get; set; }
    [JsonProperty("count_error")] public int CountError { get; set; }
    [JsonProperty("time_preset")] public string TimePreset { get; set; }
    [JsonProperty("time_first")] public string TimeFirst { get; set; }
    [JsonProperty("time_start")] public string TimeStart { get; set; }
    [JsonProperty("time_stop")] public string TimeStop { get; set; }
    [JsonProperty("email_did")] public int EmialDid { get; set; }
    [JsonProperty("email_id")] public int EmailId { get; set; }
    [JsonProperty("email_addr")] public string EmailAddr { get; set; }
    [JsonProperty("reply_to")] public string ReplyTo { get; set; }
    [JsonProperty("track")] public int Track { get; set; }
    [JsonProperty("status")] public int Status { get; set; }
    [JsonProperty("examine")] public int Examine { get; set; }
    [JsonProperty("reject_reason")] public string RejectReason { get; set; }
    [JsonProperty("task_comment")] public object TaskComment { get; set; }
    [JsonProperty("created")] public string Created { get; set; }
    [JsonProperty("updated")] public string Updated { get; set; }

}
